﻿#include"OperatorsMaterial.h"

#include "GUIFrame/PropertyWidget.h"
#include "OperatorsInterface/TreeEventOperator.h"

#include "GUIDialog/GUIPropertyDialog/MaterialEditWidget.h"
#include "GUIDialog/GUIPropertyDialog/MaterialDeleteDialog.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterial.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractMaterial.h"

namespace ModelOper
{
    OperatorsMaterial::OperatorsMaterial()
    {

    }

    OperatorsMaterial::~OperatorsMaterial()
    {

    }

    bool OperatorsMaterial::execGUI()
    {
        QWidget* widget = nullptr;
        QDialog* dialog = nullptr;

        GUI::MainWindow* mainWindow = dynamic_cast<GUI::MainWindow*>(FITKAPP->getGlobalData()->getMainWindow());
        if (mainWindow == nullptr)return false;
        GUI::PropertyWidget* propertyWidget = mainWindow->getPropertyWidget();
        if (propertyWidget == nullptr)return false;

        Interface::FITKMaterialManager* materialManager = FITKAPP->getGlobalData()->getMaterialData<Interface::FITKMaterialManager>();
        if (!materialManager)return false;//获取失败返回nullpttr，因此要检查返回值是否为空，避免空指针

        int objID = -1;
        this->argValue("objID", objID);
        switch (_operType) {
        //创建对画框获取
        case ModelOper::OperManagerBase::Create:
            widget =new GUI::MaterialEditWidget(nullptr,this,FITKAPP->getGlobalData()->getMainWindow());
            break;
        //编辑对话框获取 getParentDataID
        case ModelOper::OperManagerBase::Edit:
        {
            //QString name = materialManager->get
            Interface::FITKAbstractMaterial* material = materialManager->getDataByID(objID);
            Interface::FITKAbaMaterial* mat = dynamic_cast<Interface::FITKAbaMaterial*>(material);
            if (mat == nullptr) return false;
            widget =new GUI::MaterialEditWidget(mat,this,FITKAPP->getGlobalData()->getMainWindow());
        }
        //拷贝对话框获取
        case ModelOper::OperManagerBase::Copy:
            break;

        case ModelOper::OperManagerBase::Delete:
        {
            Interface::FITKAbstractMaterial* material = materialManager->getDataByID(objID);
            dialog= new GUI::MaterialDeleteDialog(material,this);
            break;
        }
        }
        if (mainWindow->getPropertyWidget() && widget) {
            propertyWidget->setWidget(widget);
        }

        if (dialog) {
            dialog->show();
        }

        return false;
    }

    bool OperatorsMaterial::execProfession()
    {
        // 获取模型树控制器
        auto treeOper = Core::FITKOperatorRepo::getInstance()->getOperatorT<EventOper::TreeEventOperator>("ModelTreeEvent");
        if (treeOper == nullptr) return false;
        treeOper->updateTree();

        return true;
    }
}
